//
//  RegisterViewController.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-16.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "PerfectContentViewController.h"

@interface RegisterViewController ()<RegisterViewDelegate>
{
    RegisterView *registerView;
}
@end

@implementation RegisterViewController

@synthesize viewType;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.viewType == Update_Type) {
        navigationBarView.title = @"找回密码";
    }else{
        navigationBarView.title = @"注册";
    }
    [navigationBarView setNormalLeftButton];
    
    registerView = [[RegisterView alloc] initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, CURRENT_CONTENT_WIDTH, CURRENT_CONTENT_HEIGHT-NAVIGATIONBAR_HEIGHT)];
    [registerView setDelegate:self];
    [registerView setViewType:viewType];
    [self.view addSubview:registerView];
}

-(void)leftButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - RegisterViewDelegate
-(void)done
{
//    if (self.viewType == Update_Type) {
//        [self leftButtonClick];
//    }else{
//        PerfectContentViewController *vc = [[PerfectContentViewController alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
//    }
//    return;
    
    CLog(@"确定");
    [LoadingFrameView showLoadingview];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    [dic setValue:registerView.fieldView1.textField.text forKeyPath:@"phone"];
    [dic setValue:registerView.fieldView2.textField.text forKeyPath:@"code"];
    [dic setValue:registerView.fieldView3.textField.text forKeyPath:@"password"];
    
    ASIHTTPRequest *request = nil;
    if (viewType == Register_Type) {
        [dic setValue:IOS_REGISTER forKeyPath:IOS_METHOD_NAME];
        request = [NetUtls httpGet:self withValue:dic tag:IOS_REGISTER_TAG need:YES];
    }else{
        [dic setValue:IOS_RETRIEVEPASSWORD forKeyPath:IOS_METHOD_NAME];
        request = [NetUtls httpGet:self withValue:dic tag:IOS_RETRIEVEPASSWORD_TAG need:YES];
    }
    [self superASIStartAsynchronous:request];
}

-(void)getPass
{
    CLog(@"获取验证码");
    [LoadingFrameView showLoadingview];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    [dic setValue:IOS_SENDCODE forKeyPath:IOS_METHOD_NAME];
    [dic setValue:registerView.fieldView1.textField.text forKeyPath:@"phone"];
    ASIHTTPRequest *request =  [NetUtls httpGet:self withValue:dic tag:IOS_SENDCODE_TAG need:YES];
    [self superASIStartAsynchronous:request];
}

#pragma mark - ASIHTTPRequestDelegate
-(void)requestFinished:(ASIHTTPRequest *)request
{
    
}

-(void)requestFailed:(ASIHTTPRequest *)request
{
    if (request.tag == IOS_SENDCODE_TAG) {
        [LoadingFrameView autoDisappearBlockingview:@"发送验证码失败"];
    }else if (request.tag == IOS_REGISTER_TAG) {
        [LoadingFrameView autoDisappearBlockingview:@"注册失败"];
    }else{
        [LoadingFrameView autoDisappearBlockingview:@"找回密码失败"];
    }
}

//父类获取服务器返回信息，成功获取到数据执行方法
-(void)getASIRecordSucceed:(ASIHTTPRequest *)request data:(NSDictionary*)dic
{
    if (request.tag == IOS_SENDCODE_TAG) {
        [registerView lockCode];
        [LoadingFrameView autoDisappearBlockingview:@"发送验证码成功"];
    }else if (request.tag == IOS_REGISTER_TAG) {
        //保存用户信息
        APP_USER_MSG = [[UserMsg alloc] init:dic];
        APP_USER_MSG.password = registerView.fieldView3.textField.text;
        [LoadingFrameView directlyDisappear];
        [self.navigationController pushViewController:[[PerfectContentViewController alloc] init] animated:YES];
    }else{
        [LoadingFrameView autoDisappearBlockingview:@"重设密码成功"];
        [self leftButtonClick];
    }
}

//父类获取服务器返回信息，未成功获取到数据执行方法
-(void)getASIRecordFailed:(ASIHTTPRequest *)request data:(NSString*)failed
{
    [LoadingFrameView autoDisappearBlockingview:failed];
}

@end
